local suli = fk.CreateSkill{
  name = "xiaobai__suli",
}

Fk:loadTranslationTable{
  ["xiaobai__suli"] = "肃吏",
  [":xiaobai__suli"] = "其他角色于其回合内首次使用点数不为“矩”之间的牌时，你可以对其造成1点伤害，然后你选择一项：<br>"..
  "1.用一张点数为“矩”之间的牌替换一张“矩”；<br>2.失去1点体力，然后重铸所有“矩”。",
  ["#xiaobai__suli-invoke"] = "肃吏：是否对 %dest 造成1点伤害，然后选择一项？",
  ["xiaobai__suli1"] = "用一张点数为“矩”之间的牌替换一张“矩”",
  ["xiaobai__suli2"] = "失去1点体力并重铸所有“矩”",
  ["#xiaobai__suli"] = "肃吏：请交换一张“矩”",

  ["$xiaobai__suli1"] = "朝纲隳颓，清浊淆紊，自振衣奋袂，誓肃朝庭！",
  ["$xiaobai__suli2"] = "今墨吏尸位，而贤路壅塞，吾非泰山，岂徒坐视！",
}

local GaijuBetween = function (player, number)
  local numbers = table.map(player:getPile("xiaobai__jiyan_rule"), function (id)
    return Fk:getCardById(id).number
  end)
  return (number > numbers[1] and number < numbers[2]) or (number < numbers[1] and number > numbers[2])
end

suli:addEffect(fk.CardUsing ,{
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target ~= player and player:hasSkill(self) and #player:getPile("xiaobai__jiyan_rule") == 2 and
      #Card:getIdList(data.card) == 1 then
      local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn_event == nil or turn_event.data.who ~= target then return end
      return not GaijuBetween(player, data.card.number) and
        #player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function (e)
          local use = e.data
          return use.from == target and not GaijuBetween(player, use.card.number)
        end, Player.HistoryTurn) == 1 and
        not target.dead
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {
      skill_name = suli.name, 
      prompt = "#xiaobai__suli-invoke::"..target.id}) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = suli.name,
    }
    if player.dead then return end
    local choices = {"xiaobai__suli2"}
    local cards = table.filter(player:getCardIds("he"), function (id)
      return GaijuBetween(player, Fk:getCardById(id).number)
    end)
    if #cards > 0 then
      table.insert(choices, 1, "xiaobai__suli1")
    end
    local choice = room:askToChoice(player,{
      choices = choices,
      skill_name = suli.name, 
      cancelable = false,
      all_choices = {"xiaobai__suli1", "xiaobai__suli2"}})
    if choice == "xiaobai__suli1" then
      local card_data = {
        {"xiaobai__jiyan_rule", player:getPile("xiaobai__jiyan_rule")},
        {player.general, cards}
      }
      local ids = room:askToPoxi(player,{ 
        poxi_type = suli.name,
        data = card_data, 
        cancelable = false})
      local id1, id2 = {}, {}
      for _, id in ipairs(ids) do
        if table.contains(cards, id) then
          table.insert(id1, id)
        else
          table.insert(id2, id)
        end
      end
      room:moveCards(
        {
          ids = id1,
          from = player,
          to = player,
          toArea = Card.PlayerSpecial,
          moveReason = fk.ReasonJustMove,
          specialName = "xiaobai__jiyan_rule",
          moveVisible = true,
          proposer = player,
        },
        {
          ids = id2,
          from = player,
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          fromSpecialName = "xiaobai__jiyan_rule",
          moveVisible = true,
          proposer = player,
        })
    else
      room:loseHp(player, 1, suli.name)
      if not player.dead and #player:getPile("xiaobai__jiyan_rule") > 0 then
        room:recastCard(player:getPile("xiaobai__jiyan_rule"), player, suli.name)
      end
    end
  end,
})
Fk:addPoxiMethod{
  name = "xiaobai__suli",
  prompt = "#xiaobai__suli",
  card_filter = function (to_select, selected, data, extra_data)
    if #selected < 2 then
      if #selected == 0 then
        return true
      else
        if table.contains(data[1][2], selected[1]) then
          return table.contains(data[2][2], to_select)
        else
          return table.contains(data[1][2], to_select)
        end
      end
    end
  end,
  feasible = function(selected, data)
    if data and #data == #selected and #selected == 2 then
      if table.contains(data[1][2], selected[1]) then
        return table.contains(data[2][2], selected[2])
      else
        return table.contains(data[1][2], selected[2])
      end
    end
  end,
  default_choice = function(data)
    if not data then return {} end
    local cids = {data[1][2][1], data[2][2][1]}
    return cids
  end,
}

return suli